package com.jiaobuchong.zookeeper;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

/**
 * @author zhoujie
 * Created on 2019-01-20 16:08
 */
public class ZkWatcherDemo {
    public static void main(String[] args) {
        ZkClient client = new ZkClient("localhost:2181");
        client.setZkSerializer(new MyZkSerializer());
        /**
         * create /girl/a 999 创建节点
         * set /girl/a 99 修改节点的数据
         * delete /girl/a 删除节点
         */
        // 监听 girl 节点下的 a 节点
        client.subscribeDataChanges("/girl/a", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println("----收到节点数据变化：" + o + "---");
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                System.out.println("----收到节点被删除了----");
            }
        });

        try {
            Thread.sleep(1000 * 60 * 2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
